<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace app\wechat\controller;


use think\Controller;
use think\Db;
class Wechat extends Controller
{
	public $qrcode_arr = array();

    public function __construct(){
        parent::__construct();
        Vendor('Wechat.Wx');
        $config=array(
            'token'=>'ntjr',
            'appID' =>'wx1937cead83aa1e53',
            'appsecret' =>'be13d003191020880eb0eda3d57845d5'
        );

        $this->wx = new  \wechat\Wx($config);
        $this->wx->valid();
		vendor('ApiResponse');
        $this->apiResponse = new \ApiResponse();
    }    

    
	//绑定手机页面
	public function user(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
        }
        $methon_url="https://api.weixin.qq.com/sns/oauth2/access_token";
        $params = array();
        $params['appid'] = \Wechat\Wx::$config['appID'];
        $params['secret'] =  \Wechat\Wx::$config['appsecret'];
        $params['code'] = $code;
        $params['grant_type'] = 'authorization_code';
        $callback = $methon_url . '?' . http_build_query($params);
        $get_access_token = file_get_contents($callback);
        $get_access_arr = json_decode($get_access_token, TRUE);

        $openid = $get_access_arr['openid'];
        $account = db('user')->where('openid',$openid)->field('phone,user_name,password')->find();
        
        $content = $this->wx->getUserInfo($openid);
        $this->assign('account', $account);
        $this->assign('user_info', $content);
		return $this->fetch();
    }



     //发送验证码
    public function getCode() {
        $sms_model = new \Sms();
        $phone = input('phone');
        $sms_time_out = 300;
        $code =  mt_rand(1000,9999);
        $sms_data['method'] = 'sendMsg';
        $sms_data['mobile'] = $phone;
        $sms_data['content']= "@1@=".$code;
        $sms_data['msgtype']= '2';             // 1-普通短信，2-模板短信
        $sms_data['tempid'] = 'JSM41451-0004'; // 模板编号
        $sms_data['code']   = 'utf-8';         // utf-8,gbk
        $res = $sms_model->send_sms($sms_data);
        if ($res['sms']['mt']['status'] == 0 ) {
            $info['code'] = $code;
            $info['phone'] = $phone;
            $info['time'] = time() + $sms_time_out; //有效验证时间
			$result = array(
				'state' => 200,
				'msg' => '验证码发送成功',
				'data' => $info
			);
        } else {
            $data['type'] = 1;
			$result = array(
				'state' => 406,
				'msg' => '验证码发送失败',
			);
        }
		return($result);
    }

    
	//绑定手机操作
    public function bind(){
        $phone = input('phone');//'15005150921';//$_POST['phone'];
        $openid = input('openid');//"ou3EF1m9AAR5udaLM_SH7oPZkp_E";//$_POST['openid'];
        $user_name = input('user_name');
        $password = input('password');
        $customer = Db::name("User");
        $info = $customer->where(array('phone'=>$phone,'user_name'=>$user_name))->find();

        if(!empty($info) && $info['password'] == md5($password)){
            $res = $customer->where(['id',$info['id']])->whereExp('openid', 'is null')->update(['openid'=>$openid]);
            if ($res) {
                return ['state' => 200, 'msg' => '绑定成功'];
            }            
        }
        return ['state' => 406, 'msg' => '绑定失败'];
    }

}